<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/26
 * Time: 11:20
 */

namespace Admin\Controller;

use Common\Model\BrandModel;
use Common\Model\CategoryModel;
use Common\Model\GoodsAttrModel;
use Common\Model\GoodsModel;
use Common\Model\TypeAttrModel;
use Org\Util\DataTree;

class GoodsController extends CommenController
{
	public function index(){
        $goods= (new GoodsModel())->select();
		$this->assign('goods',$goods);//分配变量
		$this->display();
	}
	/**
	 * 商品添加
	 */
	public function add(){
		if (IS_POST){
			$goodsModel = new GoodsModel();
			$res = $goodsModel->store();
//			$this->p($res);
			if($res['status']=='success'){
//				echo 1;
				$this->success($res['message'],u('index'));exit;
			}else{
//				echo 0;
				$this->error($res['message']);exit;
			}
		}
		//选择分类
		$cate =(new \Org\Util\DataTree())->tree((new CategoryModel())->select(),'cname');
		$this->assign('cate',$cate);

		//选择品牌
		$this->brand = M('brand')->order('sort')->select();
		$this->display();
	}
	/**
	 * 异步获取商品属性
	 */
	public function getAttr()
	{
		$tid = (int) $_POST['tid'];
		$where = array('tid' => $tid);
		$attr = M('type_attr')->where($where)->select();
		if ($attr === null)
		{
			echo 0;
		}
		else
		{
			echo json_encode($attr);
		}
	}

	/**
	 * 异步上传图片
	 */
	public function ajaxUpload(){
		//调用框架上传类来处理
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =     './'; // 设置附件上传根目录
		$upload->savePath  =     'Uploads/Pic/'; // 设置附件上传（子）目录
		// 上传文件
		//current  获取兼键值，在这转为一维数组
		$info   =   current($upload->upload());
		$path =  './' . $info['savepath'] . $info['savename'];
		echo $path;exit;
	}

	/**
	 * 异步删除图片
	 */
	public function ajaxDelImg()
	{
		echo 11;
		if(IS_AJAX){
			$path = $_POST['path'];
			if(file_exists($path)){
				unlink($path);
			}
		}
	}


	/**
	 * 商品修改
	 */
	public function edit(){
		$gid = I('get.gid');
//		$this->p($gid);
		//1.获取旧数据                 别名
		$oldData = (new GoodsModel())->alias('g')->join("ccshop_detail d on g.gid=d.gid")->where("g.gid={$gid}")->find();
		//将big下表大图处理成数组
		$oldData['big'] = explode(',',$oldData['big']);
//		$this->p($oldData);
		$this->assign('oldData',$oldData);

		//2.获取所有分类数据
		$cateData = (new DataTree())->tree((new CategoryModel())->select(),'cname');
//		$this->p($cateData);
        $this->assign('cateData',$cateData);

		//3.获取所有品牌数据
		$brandData = (new BrandModel())->select();
//		$this->p($brandData);
		$this->assign( 'brandData' , $brandData );

		//4.根据类型tid在类型属性表中获取对应属性数据
		$attrData = (new TypeAttrModel())->where("tid={$oldData['tid']} AND class=0")->select();
//		$this->p($attrData);
		foreach ($attrData as $k => $v){
			$attrData[$k]['tavalue'] = explode(',',$v['tavalue']);
		}
//		$this->p($attrData);
		$this->assign('attrData',$attrData);

		//5.在商品属性表中获取添加了那些属性
		$hasAttr = (new GoodsAttrModel())->where("gid={$gid}")->getField('gtid,gtvalue');
//		$this->p($hasAttr);
		$this->assign('hasAttr',$hasAttr);

		//6.处理规格数据
		$specData = (new TypeAttrModel())->alias('ta')->join("ccshop_goods_attr ga on ta.taid=ga.taid")->where("ga.gid={$gid} and ta.class=1")->select();
//		$this->p($specData);
		foreach($specData as $k=>$v){
			$specData[$k]['tavalue'] = explode(',',$v['tavalue']);
		}
//		$this->p($specData);
		$this->assign('specData',$specData);

		if (IS_POST){
			$goodsModel = new GoodsModel();
			$res = $goodsModel->store($gid);
//			$this->p($res);
			if($res['status']=='success'){
				echo 1;
				$this->success($res['message'],u('index'));exit;
			}else{
				echo 0;
				$this->error($res['message']);exit;
			}
		}
		$this->display();
	}
	/**
	 * 商品删除
	 */
	public function del(){
		$gid = I('get.gid');
//		$this->p($gid);
		$res = (new GoodsModel())->del($gid);
		if($res['status']=='success'){
			$this->success('删除商品成功');
			die;
		}else{
			$this->error('删除商品失败');
			die;
		}

	}

}